//
//  DetalleAlumno_VC.m
//  MasterUabApp
//
//  Created by Developer on 14/10/13.
//  Copyright (c) 2013 Owlab. All rights reserved.
//

#import "DetalleAlumno_VC.h"

@interface DetalleAlumno_VC ()

@end

@implementation DetalleAlumno_VC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    m_bUpdating = NO;
    m_bLoading = NO;
	// Do any additional setup after loading the view.
    
    _m_TextField_Nombre.text    =
    _m_TextField_Apellidos.text =
    _m_TextField_NIF.text       = @"";
    
    [self setupKeyboardControls:self textFields:[NSArray arrayWithObjects:
                                                 _m_TextField_Nombre,   _m_TextField_Apellidos,
                                                 _m_TextField_NIF,      nil]];
    
    [self loadData];
}

- (void) loadData
{
    if (m_AlumnoArray == nil)
    {
        m_AlumnoArray = [[AlumnoArray alloc]init:self];
    }
    m_bLoading = YES;
    [ECommon showProgressInView:self.view withText:@"LOADING..."];
    [m_AlumnoArray getAlumnoInfo:_alumnoID];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)update_Clicked:(id)sender
{
    if ([_m_TextField_Nombre.text isEqualToString:@""])
    {
        [ECommon showAlertInfo:@"El campo Nombre no puede estar vacío" title:@"Error"];
        return;
    }
    else if ([_m_TextField_Apellidos.text isEqualToString:@""])
    {
        [ECommon showAlertInfo:@"El campo Apellidos no puede estar vacío" title:@"Error"];
        return;
    }
    else if ([_m_TextField_NIF.text isEqualToString:@""])
    {
        [ECommon showAlertInfo:@"El campo NIF no puede estar vacío" title:@"Error"];
        return;
    }
    
    if (m_AlumnoArray == nil)
    {
        m_AlumnoArray = [[AlumnoArray alloc]init:self];
    }
    m_bUpdating = YES;
    [ECommon showProgressInView:self.view withText:@"UPDATING..."];
    [m_AlumnoArray updateAlumno:_alumnoID andNombre:_m_TextField_Nombre.text andApellidos:_m_TextField_Apellidos.text andNIF:_m_TextField_NIF.text];
}


- (void) syncDone:(BOOL)success object:(BaseObject*)obj
{
    if (success)
    {
        if ([obj isKindOfClass:[AlumnoArray class]])
        {
            if (m_bUpdating)
            {
                m_bUpdating = NO;
                [ECommon setProgressDoneAndHideWithText:@"Updated" andDelay:1.f];
            }
            else if (m_bLoading)
            {
                m_bLoading = NO;
                _m_TextField_Nombre.text    = m_AlumnoArray.alumno.AlumnoNombre;
                _m_TextField_Apellidos.text = m_AlumnoArray.alumno.AlumnoApellidos;
                _m_TextField_NIF.text       = m_AlumnoArray.alumno.AlumnoNIF;
                [ECommon hideProgress];
            }
        }
    }
    else
    {
        //Show error message
        if (obj!=nil && obj.msg != nil && obj.msg.length > 0)
        {
            [ECommon setProgressDetailsLabel:obj.msg];
            [ECommon setProgressErrorAndHideWithText:NSLocalizedString(@"Problem detected:", @"")];
        }
        else
        {
            [ECommon setProgressErrorAndHideWithText:NSLocalizedString(@"Connection error", @"")];
        }
    }
}

@end
